package de.xXephis.freego;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import de.xXephis.freego.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class hack extends Activity implements OnClickListener {
	
	private SharedPreferences settings;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        settings=this.getSharedPreferences("localsettings",0);
        
        final Intent intent = getIntent();
        final String action;
        if(intent!=null)
        	action = intent.getAction();
        else
        	action = "";
		
        if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
        	
			
        	setContentView(R.layout.createshortcut);
        	
			EditText eName, eId, eDescription, eUserID, ePrice, eProductID;
			EditText eChecksum, eDailyCode, eHourlyCode, eRebuyLink, eLegalNote;
			EditText eDemo, eValidDescription, eIName;
			eIName=				(EditText)this.findViewById(R.id.editIName);
			eName= 				(EditText)this.findViewById(R.id.editName);
			eId=				(EditText)this.findViewById(R.id.editID);
			eDescription=		(EditText)this.findViewById(R.id.editDescription);
			eUserID=			(EditText)this.findViewById(R.id.editUserID);
			ePrice=				(EditText)this.findViewById(R.id.editPrice);
			eProductID=			(EditText)this.findViewById(R.id.editProductId);
			eChecksum=			(EditText)this.findViewById(R.id.editChecksum);
			eDailyCode=			(EditText)this.findViewById(R.id.editDailyCode);
			eHourlyCode=		(EditText)this.findViewById(R.id.editHourlyCode);
			eRebuyLink=			(EditText)this.findViewById(R.id.editRebuyLink);
			eLegalNote=			(EditText)this.findViewById(R.id.editLegalNote);
			eDemo=				(EditText)this.findViewById(R.id.editDemo);
			eValidDescription=	(EditText)this.findViewById(R.id.editValidDescription);
			
			eIName 				.setText(settings.getString("eIName", "FreeGO!"));
			eName  				.setText(settings.getString("eName", "Einzelfahrkarte"));
			eId 				.setText(settings.getString("eID", "906935000"));
			eDescription 		.setText(settings.getString("eDescription", "gültig in 210 Halle\nZone 210"));
			eUserID 			.setText(settings.getString("eUserID", "+491781310000"));
			ePrice 				.setText(settings.getString("ePrice", "1,70"));
			eProductID 			.setText(settings.getString("eProductId", "219"));
			eChecksum 			.setText(settings.getString("eChecksum", "6007"));
			eDailyCode 			.setText(settings.getString("eDailyCode", "Elihu"));
			eHourlyCode 		.setText(settings.getString("eHourlyCode", "Ely"));
			eRebuyLink 			.setText(settings.getString("eRebuyLink", "https://hn1.the-agent-factory.de/easygo2/rest/regions/mdv/modules/payment/catalog?ticketID=191000"));
			eLegalNote 			.setText(settings.getString("eLegalNote", "Verkauf erfolgte auf Rechnung HAVAG.\nEs gelten die Tarifbestimmungen und Beförderungsbedingungen der Verkehrsunternehmen des MDV."));
			eDemo 				.setText(settings.getString("eDemo", "false"));
			eValidDescription 	.setText(settings.getString("eValidDescription", "gültig in 210 Halle"));
        	
        	
        	Button ok=(Button)findViewById(R.id.bcreateshortcut);
        	ok.setOnClickListener(this);
        	return;
        }
        else if(intent.getBooleanExtra("freego_shortcut", false))
        {
        	int ok;
        	Toast t;
        	
        	ok=mdvticket(intent);
        	if(ok!=0)
        		t=Toast.makeText(this, "OK!", 2000);
        	else
        		t=Toast.makeText(this, "Fehler! Sorry!", 2000);
        	t.show();
  
        	ShellCommand cmd = new ShellCommand();
        	cmd.su.run("su -c 'killall de.easygo; killall de.xXephis.freego';");
        	finish();
        	this.finishActivity(0);
        	return;
        }
        else
        {
	        setContentView(R.layout.main);
        }
        return;
    }	
	private int mdvticket(Intent intent) {
       try {
        	String com="";
			
			SimpleDateFormat sdf=new SimpleDateFormat("dd.MM.yyy HH:mm");
			
			Calendar calFrom = 	Calendar.getInstance();
			calFrom.add(Calendar.MINUTE, -2);
			Calendar calTo = (Calendar)calFrom.clone();

			calTo.add(Calendar.HOUR, 1);
			
			String froms=sdf.format(calFrom.getTime());
			String tos=sdf.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DSzxdsdZJPjYt/qwqGQ2+IM1xNbW8B4MbFc2SrfHsbXYYIqOdxo/7JUnspM+f7uG57P2NrXGIWQTIkbwsBxZO423ahRZ1pgf2JxB3jbHeQ5Hj4PmvFQ8M6f32fF0dnprf1yjPc72XiwQ8DaWc516VZK4og+Yh2Pctv64z6nOsZinohxztsM4xt+lvmBfTCstPj+kNc5bRec5ORB7tbvtXMMVgXaKSqQKTGjtcTBWLGHejn/w9lEL/LWPNuu8i5UtpYv1h+Y4vGZYrFjRqM04IuwZOVjcfviEKuu88RU4CCwMZ2dz42VonXTFw7u4/pynU+dSNcd5q8DTpfpYwgHmGZgtinbUnp0tbIt4fQGOANpXpqAu5SxiiLht+3S6BmLlP8676I5jfigvqy3ikmk1MDA62PzroF5dw8M+8eY485rTGL6WodVjpTxV8zzA8BFwr5tvtMf95V/+AwcFfphnRTqKnHFXehznXSDnor7M81G643zIB8n1e0iZHXocg1dOkdp1gEZX72dzXJb3uTSDKMM8Jvq5MfAAhGMecNIzb3bH4Q5/2vyB9labjN32FUs+z3UwpW7fb28ccxULuj0PP78O1gOshA01Ma2etogfvwBXktPxt1k+5tykg+vMm5d8ohmH300erDeOS/uEzna63rzXDy6C8M6jLqLoLfvEu+Ow39lO5ePtIDx1kEO47/EV4wRX1im746B6IYfTWy3KKVJblsIUnyRtLHkvizHRHseJooTNJ0TWcnWNP2dz7b8cug9Q1DDeHmfdybmW7zrq4oMrJNlyfXV4deSxj2Utn3hv3P8AprKZPedPuasAAAAASUVORK5CYII=";
			
			String pwk=(String)intent.getStringExtra("ePrice");
			if(pwk==null)
				pwk="";
			
			s="{\"tickets\":"+
				"[{\"name\":\""+intent.getStringExtra("eName")+"\",\n"+
				"\"id\":\""+intent.getStringExtra("eId")+"\",\n"+
				"\"description\":\""+intent.getStringExtra("eDescription")+"\",\n"+
				"\"userID\":\""+intent.getStringExtra("eUserID")+"\",\n"+
				"\"price\":\"Preis: "+intent.getStringExtra("ePrice")+"€\",\n"+
				"\"productID\":\""+intent.getStringExtra("eProductID")+"\",\n"+
				"\"checksum\":\""+intent.getStringExtra("eChecksum")+"\",\n"+
				"\"dailyCode\":\""+intent.getStringExtra("eDailyCode")+"\",\n"+
				"\"hourlyCode\":\""+intent.getStringExtra("eHourlyCode")+"\",\n"+
				"\"barcode\":\""+p+"\",\n"+
				"\"rebuyLink\":\""+intent.getStringExtra("eRebuyLink")+"\",\n"+
				"\"buydate\":\""+String.valueOf(calFrom.getTime().getTime())+"\",\n"+
				"\"buydateString\":\""+froms+"\",\n"+
				"\"legalNote\":\""+intent.getStringExtra("eLegalNote")+"\",\n"+
				"\"validString\":\"gültig bis "+tos+"\",\n"+
				"\"demo\":"+intent.getStringExtra("eDemo")+",\n"+
				"\"validDescription\":\""+intent.getStringExtra("eValidDescription")+"\"}],\n"+
				"\"currentMonthExpenses\":\""+intent.getStringExtra("ePrice")+"€\",\n"+
				"\"getMoreTicketsAvailable\":false,\n"+
				"\"monthlyTurnover\":"+pwk.replaceAll(",", "")+"};\n";
				

			//command
			com="su -c 'killall de.easygo; ";
			com+="mount -o remount,rw /dev/block/mtdblock3 /system && ( ";
			com+="cat \"/data/data/de.xXephis.freego/files/tickets.dat\" > \"/data/data/de.easygo/files/tickets.dat\"; ";
			com+="mount -o remount /dev/block/mtdblock3 /system; )';";
			
			FileOutputStream fOut = openFileOutput("tickets.dat",MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE);
			OutputStreamWriter fw = new OutputStreamWriter(fOut);
			fw.write(s);
			fw.flush();
			fw.close();
			
			
			ShellCommand cmd = new ShellCommand();
			//Log.d("freego cmd", "su");
			//cmd.su.run("su");
			Log.d("freego cmd", "remount/cp/remount");
			cmd.su.runWaitFor(com);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("FreeGO","Linux cmd Failed!",e);
			return 0;
		}
		return 1;
	}
	

	private void setupShortcut() {
	    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this, this.getClass().getName());
		// save/pudh them under each defined key
        shortcutIntent.putExtra("freego_shortcut", true);
		shortcutIntent.putExtra("eName", 				settings.getString("eName","Einzelfahrkarte"));
		shortcutIntent.putExtra("eId", 					settings.getString("eId","906935208"));
		shortcutIntent.putExtra("eDescription", 		settings.getString("eDescription","gültig in 210 Halle\nZone 210"));
		shortcutIntent.putExtra("eUserID", 				settings.getString("eUserID","+491781310000"));
		shortcutIntent.putExtra("ePrice", 				settings.getString("ePrice","1,70"));
		shortcutIntent.putExtra("eProductID", 			settings.getString("eProductID","219"));
		shortcutIntent.putExtra("eChecksum", 			settings.getString("eChecksum","6007"));
		shortcutIntent.putExtra("eDailyCode", 			settings.getString("eDailyCode","Elihu"));
		shortcutIntent.putExtra("eHourlyCode", 			settings.getString("eHourlyCode","Ely"));
		shortcutIntent.putExtra("eRebuyLink", 			settings.getString("eRebuyLink","https://hn1.the-agent-factory.de/easygo2/rest/regions/mdv/modules/payment/catalog?ticketID=191666"));
		shortcutIntent.putExtra("eLegalNote", 			settings.getString("eLegalNote","Verkauf erfolgte auf Rechnung HAVAG.\nEs gelten die Tarifbestimmungen und Beförderungsbedingungen der Verkehrsunternehmen des MDV."));
		shortcutIntent.putExtra("eDemo", 				settings.getString("eDemo","false"));
		shortcutIntent.putExtra("eValidDescription", 	settings.getString("eValidDescription","gültig in 210 Halle"));
		
		
        // Then, set up the container intent (the response to the caller)
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, (String)settings.getString("eIName","FreeGO!"));
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
                this,  R.drawable.icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        // Now, return the result to the launcher

        setResult(RESULT_OK, intent);
	}
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.bcreateshortcut:
			{
				EditText eName, eId, eDescription, eUserID, ePrice, eProductID;
				EditText eChecksum, eDailyCode, eHourlyCode, eRebuyLink, eLegalNote;
				EditText eDemo, eValidDescription, eIName;
				eIName=				(EditText)this.findViewById(R.id.editIName);
				eName= 				(EditText)this.findViewById(R.id.editName);
				eId=				(EditText)this.findViewById(R.id.editID);
				eDescription=		(EditText)this.findViewById(R.id.editValidDescription);
				eUserID=			(EditText)this.findViewById(R.id.editUserID);
				ePrice=				(EditText)this.findViewById(R.id.editPrice);
				eProductID=			(EditText)this.findViewById(R.id.editProductId);
				eChecksum=			(EditText)this.findViewById(R.id.editChecksum);
				eDailyCode=			(EditText)this.findViewById(R.id.editDailyCode);
				eHourlyCode=		(EditText)this.findViewById(R.id.editHourlyCode);
				eRebuyLink=			(EditText)this.findViewById(R.id.editRebuyLink);
				eLegalNote=			(EditText)this.findViewById(R.id.editLegalNote);
				eDemo=				(EditText)this.findViewById(R.id.editDemo);
				eValidDescription=	(EditText)this.findViewById(R.id.editValidDescription);
				
				Editor e=settings.edit();
				e.putString("eIName", eIName.getText().toString());
				e.putString("eName", eName.getText().toString());
				e.putString("eId", eId.getText().toString());
				e.putString("eDescription", eDescription.getText().toString());
				e.putString("eUserID", eUserID.getText().toString());
				e.putString("ePrice", ePrice.getText().toString());
				e.putString("eProductID", eProductID.getText().toString());
				e.putString("eChecksum", eChecksum.getText().toString());
				e.putString("eDailyCode", eDailyCode.getText().toString());
				e.putString("eHourlyCode", eHourlyCode.getText().toString());
				e.putString("eRebuyLink", eRebuyLink.getText().toString());
				e.putString("eLegalNote", eLegalNote.getText().toString());
				e.putString("eDemo", eDemo.getText().toString());
				e.putString("eValidDescription", eValidDescription.getText().toString());				
				
				
				e.commit();
				
				setupShortcut();
				finish();
			}break;
		}
	}
}